const { DataTypes } = require("sequelize");
const seq = require("../db/sequelize");
const Goods = require("./goodsModel");

const Comment = seq.define(
    "comment",
    {
        user_name: {
            type: DataTypes.STRING,
            allowNull: false,
            comment: "用户名",
        },
        goods_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            comment: "所属商品ID",
        },
        content: {
            type: DataTypes.TEXT,
            allowNull: false,
            comment: "评论内容",
        },
        comment_time: {
            type: DataTypes.DATE,
            allowNull: false,
            comment: "评论时间",
            defaultValue: DataTypes.NOW,
        },
        comment_star: {
            type: DataTypes.INTEGER,
            allowNull: false,
            comment: "评论星级",
        },
    }
);

// 如果需要重新建表，可以取消注释下面这行代码
// Comment.sync({ force: true });

// 定义与 Goods 的关联
Comment.belongsTo(Goods, {
    foreignKey: "goods_id",
    as: "goods_info",
});

module.exports = Comment;